package com.nmwlw.demo.data.viewModle

import android.app.Application
import android.content.Context
import android.net.wifi.WifiManager
import androidx.lifecycle.AndroidViewModel
import com.nmwlw.demo.data.state.CommonSetState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update


class WifiSetModel(application: Application) : AndroidViewModel(application){

    private val _wifiSetState = MutableStateFlow(CommonSetState());
    val wifiSetState: StateFlow<CommonSetState> = _wifiSetState.asStateFlow()

    private val wifiManager: WifiManager
    init {
        wifiManager = getApplication<Application>().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        isEnableWifi()
    }

    /**
     * 检查wifi状态
     */
    private fun isEnableWifi() {
        _wifiSetState.update {
                currentState ->
            currentState.copy(wifiEnable = wifiManager.isWifiEnabled())
        }
    }

    /**
     * 开启/关闭wifi
     */
    fun enableWifi() {

        _wifiSetState.update {
                currentState ->
            currentState.copy(wifiEnable = !(wifiSetState.value.wifiEnable))
        }
    }

    /**
     *
     */



}